<?php

namespace App\Http\Models\Report;

use App\Http\Models\Business\InvoiceTargetKeyProduct;
use App\Http\Models\Report\RegionKeyProduct;
use App\Http\Models\Business\CacheInvoiceRegionKeyProduct;

class ReportRegionKeyProduct
{
    private $year = 0;

    //设置了指标的大区
    public $sales_regions = null;
    public $cache_invoice_region_key_products = null;
    public $invoice_target_key_products = null;

    //汇总好的区分重点产品的台数，同一个大区合并到一起
    //键名，重点产品ID
    //键值，汇总台数
    private $total_target_key_product = [];
    private $total_real_key_product = [];

    public function __construct(int $year)
    {
        $this->year = $year;
        $sales_region_code_exists_target = InvoiceTargetKeyProduct::groupBy('sales_region_code')->pluck('sales_region_code')->toArray();
        $sales_regions = RegionKeyProduct::whereIn('code', $sales_region_code_exists_target)->get();
        $cache_invoice_region_key_products = CacheInvoiceRegionKeyProduct::where(['year' => $this->year])->get();
        $invoice_target_key_products = InvoiceTargetKeyProduct::where(['year' => $this->year])->get();

        $this->sales_regions = $sales_regions;
        $this->cache_invoice_region_key_products = $cache_invoice_region_key_products;
        $this->invoice_target_key_products = $invoice_target_key_products;

        $this->init();
    }



    /**
     * 初始化
     */
    private function init()
    {
        foreach ($this->sales_regions as $sales_region) {
            //分配指标
            foreach ($this->invoice_target_key_products as $value) {
                if ($sales_region->code == $value->sales_region_code) {
                    if (!array_key_exists($value->category_key_product_id, $sales_region->invoice_target_key_product)) {
                        $sales_region->invoice_target_key_product[$value->category_key_product_id] = $value->quantity;
                    }
                }
            }
            //分配实际开票数量
            foreach ($this->cache_invoice_region_key_products as $value) {
                if ($sales_region->code == $value->sales_region_code) {
                    if (!array_key_exists($value->category_key_product_id, $sales_region->invoice_real_key_product)) {
                        $sales_region->invoice_real_key_product[$value->category_key_product_id] = $value->quantity;
                    }
                }
            }
        }

        $this->setTotalTarget();
        $this->setTotalReal();
    }

    public function getTotalTargetById(int $category_key_product_id)
    {
        $result = 0;
        if (array_key_exists($category_key_product_id, $this->total_target_key_product)) {
            $result = $this->total_target_key_product[$category_key_product_id];
        }
        return $result;
    }

    public function getTotalRealById(int $category_key_product_id)
    {
        $result = 0;
        if (array_key_exists($category_key_product_id, $this->total_real_key_product)) {
            $result = $this->total_real_key_product[$category_key_product_id];
        }
        return $result;
    }


    public function getTotalProgressById(int $category_key_product_id)
    {
        $result = 0;
        $real = 0;
        if (array_key_exists($category_key_product_id, $this->total_real_key_product)) {
            $real = $this->total_real_key_product[$category_key_product_id];
        }
        $target = 0;
        if (array_key_exists($category_key_product_id, $this->total_target_key_product)) {
            $target = $this->total_target_key_product[$category_key_product_id];
        }
        if ($target != 0) {
            $result = $real / $target;
        }
        return $result;
    }


    //根据重点产品分类汇总、同一个大区加一起
    private function setTotalTarget()
    {
        foreach ($this->invoice_target_key_products as $value) {
            if (array_key_exists($value->category_key_product_id, $this->total_target_key_product)) {
                $this->total_target_key_product[$value->category_key_product_id] += $value->quantity;
            } else {
                $this->total_target_key_product[$value->category_key_product_id] = $value->quantity;
            }
        }
    }

    //根据重点产品分类汇总、同一个大区加一起
    private function setTotalReal()
    {
        foreach ($this->cache_invoice_region_key_products as $value) {
            if (array_key_exists($value->category_key_product_id, $this->total_real_key_product)) {
                $this->total_real_key_product[$value->category_key_product_id] += $value->quantity;
            } else {
                $this->total_real_key_product[$value->category_key_product_id] = $value->quantity;
            }
        }
    }
}
